{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### model predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmmodule.utils import protocols\n",
    "from bmutils.series import generate_series,SeriesClass\n",
    "from brain_ctp_mrp.brain_mrp.aibrainmrp import IntegrationPipeline\n",
    "\n",
    "data_folder = \"/Resources/Biomind_CTP_01_/\"\n",
    "all_series = generate_series(data_folder)\n",
    "mrp_series_uid = list(all_series.keys())[0]\n",
    "payload = {\n",
    "    'pdicom_folder':  data_folder,\n",
    "    'planguage': 'en-us',\n",
    "    'pconfig': {'mrp': {}},\n",
    "    'uid': mrp_series_uid,\n",
    "    'all_series': all_series,\n",
    "    'pseries_classifier': {SeriesClass.MRP.key:mrp_series_uid}\n",
    "}\n",
    "model_protocols = IntegrationPipeline(\n",
    "    args={},\n",
    "    mode='production',\n",
    "    tensorrt=tensorrt,\n",
    "    cache={}, \n",
    "    predict_config={}\n",
    ")(payload)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get series information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pydicom\n",
    "pydicom.read_file('/Resources//MRP/1.3.12.2.1107.5.2.32.35036.2015052301481365184810523.dcm')\n",
    "StudyInstanceUID = instance_info.get('ImageType') #instance_info中的标签直接结合get\n",
    "StudyInstanceUID"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### AutocastNumpyToJSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmutils.pipelines import AutocastNumpyToJSON\n",
    "result['mip_aif_pos_list'] = AutocastNumpyToJSON()(result['mip_aif_pos_list'])\n",
    "result['mip_vof_pos_list'] = AutocastNumpyToJSON()(result['mip_vof_pos_list'])\n",
    "for x in result['mip_aif_pos_list']:\n",
    "    for y in x:\n",
    "        print(type(y))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### dump data & load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "with open(\"/Resources/model_output.pkl\", \"wb\") as fn:\n",
    "    pickle.dump(res, fn)\n",
    "\n",
    "import pickle\n",
    "with open(\"/Resources/model_output.pkl\", \"rb\") as fn:\n",
    "    result = pickle.load(fn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "with open(\"/Resources/model_output.json\", \"w\") as fn:\n",
    "    json.dump(result, fn)\n",
    "    \n",
    "import json\n",
    "with open(\"/Resources/output_mrp.json\", \"r\") as fn:\n",
    "    result = json.load(fn)\n",
    "result.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### mask length & series length check"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#检测mask长度\n",
    "import numpy as np\n",
    "from bmutils.utils import str2npy\n",
    "for x in mrp_protocols['pprediction'][mrp_series_uid]:\n",
    "    for k,v in x.items():\n",
    "        if k==\"mask\":\n",
    "            print(str2npy(v).shape)\n",
    "#检测series长度\n",
    "all_series = generate_series(CTP_folder)\n",
    "ctp_series_uid = list(all_series.keys())[0]\n",
    "len(all_series[ctp_series_uid])#序列长度32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### AIEnginePredict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bmutils.pipelines import AIEnginePredict\n",
    "CTP_folder = \"/Resources/Biomind_CTP_01_/\"\n",
    "# test case\n",
    "test_case_MRP  = {\n",
    "    \"dcm_path\":MRP_folder,\n",
    "    \"study_uid\":'1.3.12.2.1107.5.2.32.35036.30000015052205525354600000139',\n",
    "    \"protocols\": {\n",
    "        'pseries_classifier': {\n",
    "             \"MRP\":'1.3.12.2.1107.5.2.32.35036.201505230148037020010505.0.0.0'\n",
    "        },\n",
    "        'penable_cached_results': False\n",
    "    }\n",
    "}\n",
    "output = AIEnginePredict(host=\"https://192.168.2.44\")([test_case[\"study_uid\"],test_case[\"protocols\"]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
